//一、使用require引入http模块，获取http请求对象
const http = require('http');
//二、设置服务器信息
//设置ip地址
const hostname = '127.0.0.1';
//设置端口号
const port = 3000;
//三、搭建服务
const server = http.createServer((request,response)=>{
    
    if(request.url == '/favicon.ico'){
        return;
    }

    console.log(request.url);

    //业务逻辑
    //设置响应头信息
    response.setHeader('Content-Type', 'text/plain;charset=utf-8' );
    //设置允许跨域
    response.setHeader("Access-Control-Allow-Origin","*");

    //定义响应结果
    let result = {
            code:200,
            msg:'连接成功',
            data:'123'
        }
    

    response.end(JSON.stringify(result));

});
//四、监听服务
//让服务器能实时获取请求
server.listen(port,hostname,()=>{
    //在服务端输出
    console.log(`Node.js Server is running on http://${hostname}:${port}  `);
});
